package com.fourforfo.fourmall.order.web;

import com.alipay.api.AlipayApiException;
import com.fourforfo.fourmall.order.config.AlipayTemplate;
import com.fourforfo.fourmall.order.service.OrderService;
import com.fourforfo.fourmall.order.vo.alipay.PayVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: xxl-sso-core
 * @description: 支付相关的请求
 * @author: fourforfo
 * @email: fourforfo@gmail.com
 * @create: 2022-09-12 16:19
 **/
@Slf4j
@Controller
public class PayWebController {

    @Autowired
    AlipayTemplate alipayTemplate;

    @Autowired
    OrderService orderService;

    /**
     * 1.将支付页让浏览器展示
     * 2.支付成功以后，我们要跳到用户的订单列表页
     * @param orderSn
     * @return
     * @throws AlipayApiException
     */
    @ResponseBody
    @GetMapping(value = "/payOrder",produces = "text/html")
    public String payOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {

        PayVO payVO = orderService.getOrderPay(orderSn);
        if(payVO == null){
            log.error("payVO为空，请查看错误原因！");
        }
        String result = alipayTemplate.pay(payVO);

        return result;
    }
}
